TRANSACTIONS PLUGIN
--------------------
Authors: Mike Barlow


Before creating a new payment gateway component, it will be worth research / testing and getting the payment gateway to work in hardcoded php before moving it into a Transaction component.
This should help understand the flow of the Payment Gateway and how / what steps are needed in order to successfully take money and correctly handle errors. Once this is known, you can then move onto move that code into a Transaction component.

Creating a new gateway should hopefully be fairly straight forward. Each gateway will need to be created as a component within this plugin. To aid with development there is a PHP interface located in Transactions/Lib/GatewayInterface.php for all components to implement. The interface (as it should) documents the functions that are required for the component to work correctly with the Transaction plugin.

For those who are not 100% up on interfaces, more information can be found here: http://uk3.php.net/interface or by ask one of the devs in the office :)

Other functions are obviously allowed within your component but these will never get run by the Transaction plugin component nor will it be aware they exist.

A basic component class for something like Paypal (currently no component exists for Paypal) would be something like this: (DocBlocks removed in this example for ease of reading)

Created in /app/Plugin/Transactions/Controller/Component/PaypalComponent.php

<?php

App::uses('Component', 'Controller');
App::import('Lib', 'Transactions.GatewayInterface');

class PaypalComponent extends Component implements GatewayInterface
{
	private $__controller = null;

	protected $_config = null;


	public function initialize(Controller $controller) {

		parent::initialize($controller);

		$this->__controller = $controller;
	}

	public function setup()
	{
		if (Configure::read('db.config') == 'live') {

			$this->_config = Configure::read('Transactions.paypal.live');
		} else {

			$this->_config = Configure::read('Transactions.paypal.dev');
		}
	}

	public function setupPayment($transaction_id, $return, $model, $amount, $items, $extra=array())
	{

	}

	public function getPayment($transaction_id)
	{

	}

	public function processReturn()
	{

	}
}

In some instances, such as the sagepay server component, we also "App::import" an API class which we store within the Transactions/Vendor directory. For access we then also create a "protected $_api = null;" variable and within the initialize() function we also initiate and setup the API like this:

	App::import('Vendor', 'Transactions.SagepayServerApi'); // Placed at the top of the file with the other App::imports


	public function initialize(Controller $controller) {

		parent::initialize($controller);

		$this->__controller = $controller;

		$this->_api = new SagePayServerApi();
	}

--------------------
CONFIGS & DB SCHEMA
--------------------

--------
CONFIGS
--------

Most gateways will need some configuration settings, place these in _assets/gateway_configs/ and name the file as the gateway_name.php (e.g. paypal.php, sagepay_server.php)
For purposes of making it easier to understand remember to place the full context of the config with the $config variable and label the lines that will need copying into their config.

E.G. _assets/gateway_configs/sagepay_server.php
<?php

$config = array(
	'Transactions' => array(

		// --- Add these lines to your config
		'sagepay_server' => array(
			'live' => array(
				'vendor' => 'VENDORNAME'
			),
			'dev' => array(
				'vendor' => 'VENDORNAME'
			)
		)
		// ------

	)
);

----------
DB SCHEMA
----------

Some gateways may also require an extra table. If any do create a simple SQL file within _assets/gateway_sql/ with a filename of the gateway. Within this simply place the CREATE TABLE code needed to generate the table along with any INSERTS that may be needed.

E.G. _assets/gateway_sql/sagepay_server.sql

SET FOREIGN_KEY_CHECKS=0;

CREATE TABLE `transactions_sagepay_servers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `VendorTxCode` varchar(50) DEFAULT NULL,
  `VPSTxId` varchar(64) DEFAULT NULL,
  `SecurityKey` varchar(10) DEFAULT NULL,
  `TxAuthNo` bigint(20) DEFAULT NULL,
  `status` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `transaction_id` int(10) NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS=1;


